package com.example.by.test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

/**
 * Created by BY on 2016/4/18.
 */
public class PullToRefresh extends ListView {
    private LayoutInflater inflater;
    View headerView;
    private int headerHeight=80;
    private float startY, stopY;

    public PullToRefresh(Context context) {
        this(context, null);
    }

    public PullToRefresh(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflater = LayoutInflater.from(context);
        init();
    }

    private void init() {
        headerView = inflater.inflate(R.layout.header, null);
        headerView.setMinimumWidth(1080);
        setPadding(0, -headerHeight, 0, 0);
        addHeaderView(headerView);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public PullToRefresh(Context context, AttributeSet attrs) {
        this(context, null, 0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startY = event.getY();
                System.out.println("startY:" + startY);
                break;
            case MotionEvent.ACTION_MOVE:
                stopY = event.getY();
                float y = stopY - startY;
                System.out.println("sub:" + y);
                setPadding(0, (int) (y-headerHeight),0,0);
                break;
            case MotionEvent.ACTION_UP:
                System.out.println("up");
                break;
        }
        return true;
    }

}
